package com.yklab.library.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期工具类
 *
 * @author Hunter
 */
public class DateUtils {

    public static SimpleDateFormat formatDate = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
    public static SimpleDateFormat formatDay = new SimpleDateFormat("d", Locale.getDefault());
    public static SimpleDateFormat formatMonthDay = new SimpleDateFormat("M-d", Locale.getDefault());
    public static SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    /**
     * 格式化日期
     *
     * @param date
     * @return 年月日
     */
    public static String formatDate(Date date) {
        return formatDate.format(date);
    }


    public static String formatTimeToStr(long time, SimpleDateFormat formate) {
        return formate.format(new Date(time));
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return 年月日 时分秒
     */
    public static String formatDateTime(Date date) {
        return formatDateTime.format(date);
    }

    /**
     * 将时间戳解析成日期
     *
     * @param timeInMillis
     * @return 年月日
     */
    public static String parseDate(long timeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        Date date = calendar.getTime();
        return formatDate(date);
    }

    /**
     * 将时间戳解析成日期
     *
     * @param timeInMillis
     * @return 年月日 时分秒
     */
    public static String parseDateTime(long timeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        Date date = calendar.getTime();
        return formatDateTime(date);
    }

    /**
     * 解析日期
     *
     * @param date
     * @return
     */
    public static Date parseDate(String date) {
        Date mDate = null;
        try {
            mDate = formatDate.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return mDate;
    }

    /**
     * 解析日期
     *
     * @param datetime
     * @return
     */
    public static Date parseDateTime(String datetime) {
        Date mDate = null;
        try {
            mDate = formatDateTime.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return mDate;
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前日
     *
     * @return
     */
    public static int getCurrentDay() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据年 月 获取对应的月份 天数
     */
    public static int getDaysByYearMonth(int year, int month) {

        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 时间控件的年份列表
     *
     * @return
     */
    public static ArrayList<String> getYeasList() {
        ArrayList<String> dateList = new ArrayList<String>();
        int curYear = getCurrentYear();
        for (int i = 1970; i <= curYear; i++) {
            dateList.add(i + "");
        }
        return dateList;
    }

    /**
     * 获取年对应的月份
     * 以当前日期为结点
     */
    public static ArrayList<String> getMonthList(String yearKey, boolean isAll) {
        ArrayList<String> monthlist = new ArrayList<String>();
        int currentYear = getCurrentYear();
        int currentMonth = getCurrentMonth();
        int monthSize = 1;
        if (StringUtils.isEmpty(yearKey)) {
            yearKey = currentYear + "";
        }
//        if ( Integer.parseInt (yearKey)==currentYear && isAll) {
//            monthSize=currentMonth;
//        } else {
        monthSize = 12;
//        }
        for (int i = 1; i <= monthSize; i++) {
            if (i <= 9) {
                monthlist.add("0" + i);
            } else {
                monthlist.add(i + "");
            }
        }
        return monthlist;
    }

    /**
     * 获取月份对应的日
     * 以当前日期为结点
     */
    public static ArrayList<String> getDayList(String yearKey, String monthKey) {
        ArrayList<String> daylist = new ArrayList<String>();
        int currentYear = getCurrentYear();
        int currentMonth = getCurrentMonth();
        int currentDay = getCurrentDay();
        int daySize = 1;
        if (StringUtils.isEmpty(yearKey)) {
            yearKey = currentYear + "";
        }
        if (StringUtils.isEmpty(monthKey)) {
            monthKey = currentMonth + "";
        }
//        if(Integer.parseInt (monthKey)==currentMonth && Integer.parseInt (yearKey)==currentYear){
//            daySize=currentDay;
//        }else{
        daySize = getDaysByYearMonth(Integer.parseInt(yearKey), Integer.parseInt(monthKey));
//        }
        for (int i = 1; i <= daySize; i++) {
            if (i <= 9) {
                daylist.add("0" + i);
            } else {
                daylist.add(i + "");
            }

        }
        return daylist;
    }


}
